package ldh.im.fxbase.util;

import java.lang.ref.SoftReference;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;

public class SoftMapUtil {

    private static final Map<String, SoftReference<?>> softMap = new ConcurrentHashMap<>();
    private static final Map<String, Object> weakMap = new WeakHashMap<>();
//    private static final Map<String, RegionUtil.FxmlBean> weakMap = new WeakHashMap<>();

    public static <T> T getCache2(String key) {
        SoftReference<?> softReference = softMap.get(key);
        if (softReference == null) return null;
        T t = (T) softReference.get();
        return t;
    }

    public static void cache2(String key, Object object) {
        SoftReference<?>softReference = new SoftReference<>(object);
        softMap.put(key, softReference);
    }

    public static <T> T getCache(String key) {
        T data = (T) weakMap.get(key);
        if (data == null) return null;
        return data;
    }

    public static void cache(String key, Object object) {
        weakMap.put(key, object);
    }

    public static <T> T getWeakCache(String key) {
        T data = (T) weakMap.get(key);
        if (data == null) return null;
        return data;
    }

    public static void weakCache(String key, Object object) {
        weakMap.put(key, object);
    }

//    public static RegionUtil.FxmlBean getWeakCache(String key) {
//        RegionUtil.FxmlBean fxmlBean = weakMap.get(key);
//        return fxmlBean;
//    }
//
//    public static void cacheWeak(String key, RegionUtil.FxmlBean fxmlBean) {
//        weakMap.put(key, fxmlBean);
//    }
}
